﻿using System;
using System.Collections;

namespace YArchitech.HCLoad.XmlData
{
	public class RoomCollect : Hashtable
	{
		public RoomCollect(Floor floor)
		{
			this.parent = floor;
		}

		public Floor Parent
		{
			get
			{
				return this.parent;
			}
			set
			{
				this.parent = value;
			}
		}

		private void AddRoom()
		{
			try
			{
				Room room = new Room(this);
				int num = this.parent.Parent.Parent.Parent.Parent.Parent.AddCurrID();
				room.ID = num;
				this.Add(num, room);
				this.parent.Parent.Parent.Parent.Parent.Parent.hCurrIDList.Add(num, room);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public int AddRoom(Room room)
		{
			int result;
			try
			{
				room.Parent = this;
				int num = this.parent.Parent.Parent.Parent.Parent.Parent.AddCurrID();
				room.ID = num;
				this.Add(num, room);
				this.parent.Parent.Parent.Parent.Parent.Parent.hCurrIDList.Add(num, room);
				result = num;
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public void RemoveRoom(int ID)
		{
			try
			{
				this.Remove(ID);
				this.parent.Parent.Parent.Parent.Parent.Parent.hCurrIDList.Remove(ID);
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
		}

		public Room GetRoom(int ID)
		{
			Room result;
			try
			{
				result = (Room)this[ID];
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		public bool LoadRoom(Room room)
		{
			bool result;
			try
			{
				room.Parent = this;
				int id = room.ID;
				if (this.parent.Parent.Parent.Parent.Parent.Parent.hCurrIDList.ContainsKey(id))
				{
					result = false;
				}
				else
				{
					if (id > this.parent.Parent.Parent.Parent.Parent.Parent.CurrID)
					{
						this.parent.Parent.Parent.Parent.Parent.Parent.CurrID = id;
					}
					this.Add(id, room);
					this.parent.Parent.Parent.Parent.Parent.Parent.hCurrIDList.Add(id, room);
					result = true;
				}
			}
			catch (Exception ex)
			{
				throw new Exception(ex.ToString());
			}
			return result;
		}

		private Floor parent;
	}
}
